package com.onec.service.channel.service.api;


import com.onec.service.api.dto.request.channel.ChannelTradeReq;
import com.onec.service.api.dto.response.channel.ChannelTradeResp;
import com.onec.service.api.service.channel.ChannelTradeApiService;
import com.onec.service.api.utils.ValidatorUtils;
import com.onec.service.channel.entity.ChannelInfo;
import com.onec.service.channel.entity.ChannelOrderDetail;
import com.onec.service.channel.service.ChannelInfoService;
import com.onec.service.channel.service.ChannelOrderDetailService;
import com.onec.service.channel.service.channel.base.BaseChannelOrderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;


/**
 * 一个通道会分配多个商户信息：channelFlag 唯一，同时也是spring 中 bean 的方法名字：
 * merchant_{channelFlag} 和 order_{channelFlag}
 *
 * @see
 */
@DubboService(version = "1.0.1", timeout = 6000)
public class ChannelTradeApiServiceImpl implements ChannelTradeApiService {

    private static final Logger logger = LoggerFactory.getLogger(ChannelTradeApiServiceImpl.class);

    @Autowired
    ChannelInfoService channelInfoService;
    @Autowired
    ChannelOrderDetailService channelOrderDetailService;
    @Autowired
    ApplicationContext applicationContext;

    private static final String TRADE_ = "trade";

    private String getTradeChannelFlag(String channelFlag) {
        return TRADE_ + channelFlag;
    }

    /**
     * 请求微信服务！
     *
     * @param channelTradeReq
     * @return
     */
    @Override
    public ChannelTradeResp requestWeiXin(ChannelTradeReq channelTradeReq) {

        ValidatorUtils.validateEntity(channelTradeReq);
        // 创建通道订单记录：
        ChannelInfo channelInfo = channelInfoService.getChannelInfoByNo(channelTradeReq.getChannelNo());
        ChannelOrderDetail channelOrderDetail = channelOrderDetailService
                .createChannelOrderDetail(channelTradeReq, channelInfo);

        String channelBeanName = this.getTradeChannelFlag(channelInfo.getChannelFlag());
        BaseChannelOrderService baseChannelOrderService = applicationContext
                .getBean(channelBeanName, BaseChannelOrderService.class);
        return baseChannelOrderService.requestWeiXin(channelOrderDetail);
    }

    @Override
    public ChannelTradeResp requestAliPay(ChannelTradeReq channelTradeReq) {
        String channelBeanName = this.getTradeChannelFlag("");

        return null;
    }

    @Override
    public ChannelTradeResp requestUnionPay(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp openProduct(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp requestApiConfirm(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp signUpCard(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp unSignUpCard(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp rePayment(ChannelTradeReq channelTradeReq) {
        return null;
    }

    /**
     * 通道 返回：html |url
     *
     * @param channelTradeReq
     * @return ChannelTradeResp
     */
    @Override
    public ChannelTradeResp requestHtml(ChannelTradeReq channelTradeReq) {
        ValidatorUtils.validateEntity(channelTradeReq);
        // 创建通道订单记录：
        ChannelInfo channelInfo = channelInfoService.getChannelInfoByNo(channelTradeReq.getChannelNo());
        ChannelOrderDetail channelOrderDetail = channelOrderDetailService
                .createChannelOrderDetail(channelTradeReq, channelInfo);

        String channelBeanName = this.getTradeChannelFlag(channelInfo.getChannelFlag());
        BaseChannelOrderService baseChannelOrderService = applicationContext
                .getBean(channelBeanName, BaseChannelOrderService.class);
        return baseChannelOrderService.requestHtml(channelOrderDetail);
    }

    @Override
    public ChannelTradeResp querySignUpCard(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp queryOnlineOrderApi(ChannelTradeReq channelTradeReq) {
        return null;
    }

    @Override
    public ChannelTradeResp queryRepayment(ChannelTradeReq channelTradeReq) {
        return null;
    }
}
